
// 1、无头单向非循环链表实现
public class SList {

    private ListNode phead;

    public ListNode getPhead() {
        return phead;
    }

    private int size;


    //头插法
    public void addFirst(int data){
        ListNode listNode=new ListNode(data);
        listNode.next=phead;
        phead=listNode;
        size++;
    }


    //尾插法
    public void addLast(int data){
        ListNode pcur=phead;
        ListNode listNode=new ListNode(data);
        if (pcur==null){
            phead=listNode;
            size++;
            return;
        }
        while (pcur.next!=null){
            pcur=pcur.next;
        }
        pcur.next=listNode;
        size++;
    }


    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if (index<0||index>=this.size){
            System.out.println("位置非法！");
            return false;
        }
        if (index==0){
            addFirst(data);
            return true;
        }
        ListNode listNode=new ListNode(data);
        int pos=0;
        ListNode pcur=phead;
        while (pos++<index-1){
            pcur=pcur.next;
        }
        listNode.next=pcur.next;
        pcur.next=listNode;
        size++;
        return true;
    }


    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode pcur=phead;
        while (pcur!=null){
            if (pcur.val==key)
                return true;
            pcur=pcur.next;
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (phead==null){
            System.out.println("链表为空！");
            return;
        }
        ListNode pcur=phead;
        if (phead.val==key){
            phead=phead.next;
            size--;
            return;
        } else if (size==1) {
            System.out.println("未找到 "+key+" 结点！");
            return;
        }
        while (pcur.next.next!=null){
            if (pcur.next.val==key)
                break;
            pcur=pcur.next;
        }
        if (pcur.next.next==null){
            if (pcur.next.val==key){
                pcur.next=null;
                size--;
                return;
            }else {
                System.out.println("未找到 "+key+" 结点！");
                return;
            }
        }
        pcur.next=pcur.next.next;
        size--;
    }


    private boolean Allremove(int key){
        if (phead==null){
            return false;
        }
        ListNode pcur=phead;
        if (phead.val==key){
            phead=phead.next;
            size--;
            return true;
        } else if (size==1) {
            return false;
        }
        while (pcur.next.next!=null){
            if (pcur.next.val==key)
                break;
            pcur=pcur.next;
        }
        if (pcur.next.next==null){
            if (pcur.next.val==key){
                pcur.next=null;
                size--;
                return true;
            }else {
                return false;
            }
        }
        pcur.next=pcur.next.next;
        size--;
        return true;
    }


    //删除所有值为key的节点
    public void removeAllKey(int key){
        while(Allremove(key));
    }
    public ListNode removeElements(ListNode head, int val) {
        if(head==null)
            return null;
        ListNode pr=head;
        ListNode ps=pr.next;
        while(ps!=null){
            if(ps.val==val){
                if(ps.next==null){
                    pr.next=null;
                    break;
                }
                ps=ps.next;
            }else{
                pr.next=ps;
                pr=pr.next;
                ps=ps.next;
            }
        }
        if(head.val==val)
            head=head.next;
        return head;
    }


    //得到单链表的长度
    public int size(){
        return this.size;
    }


    public void display(){
        ListNode pcur=phead;
        while (pcur!=null){
            System.out.print(pcur.val+" ");
            pcur=pcur.next;
        }
        System.out.println();
    }


    public void clear(){
        while (phead!=null){
            ListNode listNode=phead;
            phead=phead.next;
            listNode.next=null;
        }
        this.size=0;
    }

}

class ListNode{

    public int val;

    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
